<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
        <%@include file="/WEB-INF/ibnms/common/base.jsp" %>
        <%@include file="/WEB-INF/ibnms/common/ztree.jsp" %>
        <script type="text/javascript">
            var tree;
            $(function(){
                var currentId='<s:property default="" value="#parameters.id"/>', rootNode=<s:property default="[]" escape="false" value="rootId"/>;
                var onclick='<s:property default="" value="#parameters.onclick"/>';
				function refreshNode(node, tree, callback){
					var parentNode = node.getParentNode();
					var realOnAsyncSuccess = tree.setting.callback.onAsyncSuccess;
					tree.setting.callback.onAsyncSuccess = function(event, treeId, treeNode, msg){
						tree.setting.callback.onAsyncSuccess = realOnAsyncSuccess;
						node = tree.getNodeByParam("id", node.id, treeNode);
						$("#"+node.tId+"_switch").click();
						tree.selectNode(node);
						if($.isFunction(callback)){
							callback.call(node, node, tree);
						}
					}
					tree.reAsyncChildNodes(parentNode, "refresh", false);
				}
                tree = $.fn.zTree.init($("#asyncTree"), {
                    async: {
                        enable: true,
                        url:"<s:url action="%{subNodesJsonAction}"/>",
                        autoParam:["id"],
                        dataFilter: function(treeId, parentNode, childNodes) {
                            return childNodes;
                        }
                    },
                    data:{
                        key:{
                            name:'name',
                            title:'name'
                        },
                        simpleData:{
                            idKey:'id'
                        }
                    },
                    callback:{
                        onNodeCreated:function(event, treeId, treeNode){
                            var id = treeNode.id;
                            if(id==currentId){
								$("#"+treeNode.tId+"_switch").click();
                                $("#"+treeNode.tId+"_a").click();
                            }else if(id.length<currentId.length && id==currentId.substr(0, id.length) && '-'==currentId.substr(id.length, 1)){
                                $("#"+treeNode.tId+"_switch").click();
                            }
                        },
						onClick:function(event, treeId, treeNode) {
							var f = window.parent[onclick];
							if(f && typeof f=="function"){
								treeNode.refreshNode=function(callback){
									refreshNode.call(treeNode, treeNode, tree, callback);
								}
								f.call(treeNode, treeNode, tree);
							}
						}
					}
				},rootNode);
				tree.expandNode(tree.getNodes()[0]);
			});
        </script>
    </head>
    <body>
        <ul id="asyncTree" class="ztree"></ul>
    </body>
</html>
